package com.sdkx.app.util;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sdkx.app.R;

/** * Toast统一管理类 */
public class ToastUtil {

    public static boolean isShow = true; /* cannot be instantiated */

    private ToastUtil() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    /** * 短时间显示Toast * * @param context * @param message */
    public static void showShort(Context context, CharSequence message) {
        if (isShow)
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }

    /** * 短时间显示Toast * * @param context * @param message */
    public static void showShort(Context context, int message) {
        if (isShow)
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }

    /** * 长时间显示Toast * * @param context * @param message */
    public static void showLong(Context context, CharSequence message) {
        if (isShow)
            Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    }

    /** * 长时间显示Toast * * @param context * @param message */
    public static void showLong(Context context, int message) {
        if (isShow)
            Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    }

    /** * 自定义显示Toast时间 * * @param context * @param message * @param duration */
    public static void show(Context context, CharSequence message, int duration) {
        if (isShow)
            Toast.makeText(context, message, duration).show();
    }

    /** * 自定义显示Toast时间 * * @param context * @param message * @param duration */
    public static void show(Context context, int message, int duration) {
        if (isShow)
            Toast.makeText(context, message, duration).show();
    }
    public static void show(Context context, String message,int gravity){
        Toast toast = Toast.makeText(context,
                message, Toast.LENGTH_SHORT);
        toast.setGravity(gravity, 0, 0);
        LinearLayout layout = (LinearLayout) toast.getView();
        layout.setBackgroundColor(Color.parseColor("#FFFFFF"));
        TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
        v.setTextColor(Color.BLACK);
        v.setTextSize(25);
        toast.show();
    }
    //自定义toast显示效果，因为旧平台硬件dpi较低，显示效果很差，需要自定义
    public static void show(Context context, String message){
        View view = LayoutInflater.from(context).inflate(R.layout.custom_toast,null);
        TextView textView = (TextView)view.findViewById(R.id.textView);
        textView.setText(message);
        Toast toast = new Toast(context);
        toast.setGravity(Gravity.BOTTOM,0,50);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
        toast.show();
    }
}
